package com.mediacloud.app.newsmodule.fragment.ebook

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.assembly.util.dp2px
import com.mediacloud.app.assembly.util.load
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.activity.microlive.BaseRecyclerAdapterX
import org.jetbrains.anko.Orientation
import org.jetbrains.anko.textColor

class EbookListAdapter(context: Context?) : BaseRecyclerAdapterX<EbookData>(context) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return EbookHolder(inflate(R.layout.item_ebook_list, parent), context)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        if (holder is EbookHolder) {
            holder.setBaseNewsItemData(getItem(position), position)
        }
    }

}

class EbookHolder(itemView: View, context: Context) : RecyclerView.ViewHolder(itemView) {
    var context = context;
    var logo: ImageView = itemView.findViewById(R.id.logo)
    var title: TextView = itemView.findViewById(R.id.title)
    var text_desc: TextView = itemView.findViewById(R.id.text_desc)
    var tvScore: TextView = itemView.findViewById(R.id.tvScore)
    var tvAuthor: TextView = itemView.findViewById(R.id.tvAuthor)
    var tvHit: TextView = itemView.findViewById(R.id.tvHit)
    var typeList: RecyclerView = itemView.findViewById(R.id.typeList)


    fun setBaseNewsItemData(item: EbookData, position: Int) {

        title.text = item.title
        tvAuthor.text = "作者: ${item.author}"
        tvScore.text = "${item.score}分"
        logo.load(item.imagePath)
        text_desc.text = item.summary
        tvHit.text = "${item.hitCount}人阅读"
        item.types?.let{
            typeList.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
            val typeItemAdapter = TypeItemAdapter()
            typeList.adapter = typeItemAdapter
            typeItemAdapter.setNewData(it)
        }

    }

    class TypeItemAdapter:BaseQuickAdapter<String,BaseViewHolder>(R.layout.item_type){
        override fun convert(helper: BaseViewHolder, item: String?) {
            helper.setText(R.id.tvType,item)
        }

    }
}